/**
 * 	 DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *   @author Colin Redmond
 *   Copyright (C) 2011 Colin Redmond
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

package com.cred.industries.platform.business.facade;

import java.util.Date;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.cred.industries.core.exceptions.ApplicationException;
import com.cred.industries.core.server.ServerConfig;
import com.cred.industries.core.server.ServerConfigEnum;
import com.cred.industries.core.transferObj.result.ReturnCodes;
import com.cred.industries.platform.business.objects.CustomerBO;
import com.cred.industries.platform.business.objects.ForgotPasswordBO;

public class MailerFacade {
	
	//private final static Logger logger = LoggerFactory.getLogger(MailerFacade.class);
	
	public static void forgotMyPasswordEmail(ForgotPasswordBO forgotPassBO, CustomerBO custBO) {
		
		try {
			String emailFrom = "";
			String emailSubject = "";
			String emailSMPT = "";
			emailFrom = ServerConfig.getInstance().getValue(String.class, ServerConfigEnum.FORGOT_EMAIL_FROM);
			emailSubject = ServerConfig.getInstance().getValue(String.class, ServerConfigEnum.FORGOT_EMAIL_SUBJECT);
			emailSMPT = ServerConfig.getInstance().getValue(String.class, ServerConfigEnum.FORGOT_EMAIL_SMTP);
			
	        Properties properties = new Properties();

	        // If using Transport.send(),
	        // need to specify which host to send it to
	        properties.put("mail.smtp.host", emailSMPT);
	        // To see what is going on behind the scene
	        properties.put("mail.debug", "true");
	        Session session = Session.getInstance(properties);

	        Message msg = new MimeMessage(session);

            //Set message attributes
            msg.setFrom(new InternetAddress(emailFrom));
            InternetAddress[] address = {new InternetAddress(custBO.getEmailAddress())};
            msg.setRecipients(Message.RecipientType.TO, address);
            msg.setSubject(emailSubject);
            msg.setSentDate(new Date());
            
            //should have better details about where coming from. 
            //msg should not be in code
            String html = "<p>Hello %1$s</p> <br/> " +
            		"<p>You are receiving this email because you have reported that you have lost your password. If you did not request this email then please ignore it <br/>" +
            		"to activate your temporary password you must follow the link below. Your existing password will remain valid until you click this link</p> <br/>" +
            		"<a href=\"%2$s\"> %2$s </a>" +
            		"<p>This link will expire after %5$d hours. After that you can still use your original password or you can request a new password again</p>" +
            		"<p> ------------------------------------------------ <br/>" +
            		"YOUR LOGIN INFORMATION <br/>" +
            		"------------------------------------------------ </p>" +
            		"<p>Your persona name is: %3$s</p>" +
            		"<p>Your new temporary password is: %4$s</p>";
            
            String url = "http://localhost:8080/rest/PasswordReset.jsp?personaName=" + custBO.getPersonaName() + "&passId=" + forgotPassBO.getTempPasswordID();
            
            String htmlMsg = String.format(html, custBO.getPersonaName(), url, custBO.getPersonaName(), forgotPassBO.getNewPassword(), forgotPassBO.defaultValidForHours());

            // set the HTML message content
            msg.setContent(htmlMsg, "text/html");

            //Send the message
            Transport.send(msg);	
			
		} catch (MessagingException mex) {
            // Prints all nested (chained) exceptions as well
			throw new ApplicationException(mex, ReturnCodes.UNKNOWN_ERROR, "Failed to send email ");
        }
	}

}
